-- cannon
-- create by panyl
-- 大炮

return {
    getTargets = function(trapId, pos, targetPos)
        local info = CommonTrapM.query(trapId);
        local trapType = info["type"];
        local feature = info["feature"];

        assert(trapType == TRAP_TYPE_CANNON, "不是大炮！");

        local radius = info["radius"];
        local maxRadius = math.max(DUNGEON_HEIGHT, DUNGEON_WIDTH);
        if type(radius) ~= "number" or
            radius == NUMBER_INFINITY or
            radius > maxRadius then
            radius = maxRadius;
        end

        local ret = {};
        local coor = DungeonM.convertToCoor(pos);
        local x = coor["x"];
        local y = coor["y"];
        local grid =  DungeonM.getGridByPos(pos);
        local direction = grid.direction;
        local vector = CommonTrapM.getVector(direction);

        for dist = 1, radius do
            local xx = x + vector[1] * dist;
            local yy = y + vector[2] * dist;

            -- 超出边界了
            if xx <= 0 or xx > DUNGEON_WIDTH or yy <= 0 or yy > DUNGEON_HEIGHT then
                break;
            end

            repeat
                pos = DungeonM.convertToPos(xx, yy);
                grid = DungeonM.getGridByPos(pos);
                local monster = grid.monster;

                -- 是否无视目标的
                if  not grid:isOpened() and
                    bit.band(feature, TRAP_FEATURE_IGNORE_BLOCK) ~= TRAP_FEATURE_IGNORE_BLOCK then
                    break;
                end

                -- 如果是不可穿透的（只能攻击最前面的敌人）
                if (#ret > 0 and
                    bit.band(feature, TRAP_FEATURE_NOT_THROUGH) == TRAP_FEATURE_NOT_THROUGH) then
                    return ret;
                end

                -- -- 已翻开且没有怪物的格子就不管了
                -- if  grid:isOpened() and
                --     (not grid:isMonster() or
                --      not monster or
                --      monster:isDead()) then
                --     break;
                -- end

                table.insert(ret, grid);
            until true;
        end

    -- 战争狂专属
    local prop = PropM.combine(ME.user, "strangelove", 76);
    if prop[3] > 0 then
        -- 触发特技发动事件
        EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
    end

        return ret;
    end,
};
